import { _ } from "../../utils/sensorsdata.min";

// components/footer/footer.js
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    scriptionNoticeStatus: {
      type: Boolean,
      value: false
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
   
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //订阅消息
    subscriptionNotice() {
      let that = this;
      let tmps = ["kzHotWcGrS-2dVnQne4HpeXOLW1nUVQHY4pZvaeXUIk", 'z_FCi83NHKla98jcdIlMNvSh2ybN1YAgeSSJgvR7tq8'];
      if (wx.requestSubscribeMessage) {
        wx.requestSubscribeMessage({
          tmplIds: tmps,
          success(res) { 
            //判断是否所有消息都已订阅，引导开启未订阅消息
            if (that.checkAllAccept(res, tmps)) {
                that.triggerEvent('subscribeEvent');
            } else {
              //引导开启
              that.showModel('是否开启所有订阅', '检测到含有未开启订阅，\n是否去开启所有订阅？')
            }
          }, fail(res) {
            //引导询问开启消息订阅
            if (res.errCode == 20004) {
              that.showModel('是否授权开启消息订阅', '为了能够准确通知您的咨询情况，请点击确认');
            } else {
              wx.showToast({
                title: '订阅失效'
              })
            }
          }
        });
      } else {
        //兼容处理
        wx.showModal({
          title: '提示',
          content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
        });
      }
    },
    //取消订阅
    onCancle(){
      this.triggerEvent('hideSubscribeEvent');
    },
    //判断是否订阅所有消息
    checkAllAccept(res, id) {
      if (id.length > 0) {
        id.forEach(ele => {
          if (!res[ele]) {
            return false;
          }
        });
        return true;
      }
    },
    //引导用户开启
    showModel(titleStr, contentStr) {
      wx.showModal({
        title: titleStr,
        content: contentStr,
        success(res) {
          if (res.cancel) {

          } else {
            wx.openSetting({
              success() {

              }
            })
          }
        }
      })
    }
  }
})
